package com.example.lopngoaingu;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class HandlerActivity extends Activity {

	private Button btn;
	private Thread t;
	private Handler hander;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.handler_layout);
		btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				t.start();
			}
		});

		hander = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				Toast.makeText(getApplicationContext(),
						"Show message here " + msg.what, Toast.LENGTH_SHORT)
						.show();
			}
		};

		t = new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					for (int i = 0; i < 10; i++) {
						hander.sendEmptyMessage(i);
						Thread.sleep(500);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};

		Runnable r = new Runnable() {

			@Override
			public void run() {
				for (int i = 0; i < 10; i++) {
					Toast.makeText(getApplicationContext(), "" + i,
							Toast.LENGTH_SHORT).show();
				}
			}
		};

		Handler h = new Handler();
		h.post(r);
	}
}
